
namespace ClassDecorator {
  export interface Person {
    name: string
    // address: string
  }

  /**
   * 调用在构造函数之前
   * @param target 充分必要参数，为其所装饰的类本身
   * @param context 上下文对象
   */
  function enhancer(target: any, context: any) {
    // 给被装饰类新增打印方法
    target.prototype.printFunction = function(value:string){
      console.log("装饰器新增打印方法：" + value)
    }

    // 打印被装饰的类名
    console.log('class enhancer ' + context.name)
    console.log('target:' + target)
    console.log('context:' + context.kind)
    target.prototype.name = 'TS 装饰器'
    target.prototype.value = "装饰器添加的值"
    // target.prototype.address = 'ECMAScript 2022'
  }

  @enhancer // 普通装饰器
  export class Person {
    printFunction(arg0: string) {
      // throw new Error("Method not implemented.")
    }
    constructor() {
      console.log("Person 构造器被调用")
    }
  }

  @enhancer
  export class ClassDemo{
    name:string

    constructor(){
      this.name = ""
      console.log("ClassDemo 构造器被调用")
    }

  }

}

const person = new ClassDecorator.Person()
console.log(person.name)
person.printFunction("person")
// console.log(person.value)
// console.log(person.address)

const classDemo = new ClassDecorator.ClassDemo()
console.log(classDemo.name)

